package com.ews.onlineexchange.controller.activitii;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

import javax.enterprise.context.ConversationScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseId;
import javax.inject.Inject;
import javax.inject.Named;

import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.activiti.image.ProcessDiagramGenerator;
import org.apache.commons.lang3.StringUtils;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

@Named
@ConversationScoped
public class BpmHome implements Serializable {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 1L;
	
	@Inject
	RuntimeService 				runtimeService;
	
	@Inject
	RepositoryService 			repositoryService;
	
	@Inject
	TaskService					taskService;
	
	@Inject
	ProcessEngine				processEngine;

	private String				processId;
	private String				taskId;
	private Task				task;
	private String 				businessKey;

	public String getProcessId() {
		return processId;
	}

	public void setProcessId( String processId ) {
		this.processId = processId;
	}

	public String getTaskId() {
		return taskId;
	}

	public void setTaskId( String taskId ) {
		this.taskId = taskId;
	}
	
	public Task getTask() {
		if ( this.task == null ) {
			if ( StringUtils.isBlank( getTaskId() ) ) {
				return null;
			}
			TaskQuery taskQuery = taskService.createTaskQuery();
			taskQuery.includeProcessVariables();
			task = taskQuery.taskId( getTaskId() ).singleResult();
		}		
		return task;
	}
	
	public void setTask( Task task ) {
		this.task = task;
	}

	public String getBusinessKey() {
		return businessKey;
	}

	public void setBusinessKey( String businessKey ) {
		this.businessKey = businessKey;
	}

	/**
	 * Get Process instance diagram
	 */
	public InputStream getProcessDiagram( String businessKey ) {
		ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
				.processInstanceBusinessKey( businessKey ).singleResult();

		// null check
		if ( processInstance != null ) {
			// get process model
			BpmnModel model = repositoryService.getBpmnModel( processInstance.getProcessDefinitionId() );

			if ( model != null && model.getLocationMap().size() > 0 ) {
		        ProcessDiagramGenerator diagramGenerator = new CustomProcessDiagramGenerator();
		        return diagramGenerator.generateDiagram( model, "jpg", runtimeService.getActiveActivityIds( processInstance.getId() ) );
			}
		}
		return null;
	}

    public StreamedContent getProcessDiagramImage() throws IOException {    	
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            // So, we're rendering the view. Return a stub StreamedContent so that it will generate right URL.
            return new DefaultStreamedContent();
        } else {        	
            // So, browser is requesting the image. Return a real StreamedContent with the image bytes.
        	Object businessKey = context.getExternalContext().getRequestParameterMap().get("businessKey");
            return new DefaultStreamedContent( getProcessDiagram( (String) businessKey ) );
        }
    }
    
    public String deleteTask() {
    	taskService.complete(getTaskId());
//    	runtimeService.get
    	return  "CancelApplyTaskList.xhtml?processKey=applyCancelAudit&amp;faces-redirect=true&amp;includeViewParams=true";
    }

}
